//
//  SLLabelFeild.m
//  StarLight
//
//  Created by Duke.wu on 16/5/24.
//  Copyright © 2016年 Duke.Wu. All rights reserved.
//

#import "SLLabelFeild.h"

@interface SLLabelFeild ()<UITextFieldDelegate,UITextViewDelegate>

@end

@implementation SLLabelFeild


- (instancetype)initWithtitle:(NSString *)title{
  
  self = [super init];
  if (self) {
    self.backgroundColor = [UIColor whiteColor];
    self.titleLabel = [UILabel labelWithFontSize:16 textColor:SLInkColor text:title];
    self.titleLabel.textAlignment = NSTextAlignmentRight;
    
    self.contentField = [[UITextField alloc] init];
    self.contentField.delegate = self;
    
    self.contentTextView = [[UITextView alloc] init];
    self.contentTextView.font = [UIFont systemFontOfSize:16];
    self.contentTextView.delegate = self;
    
    [self addSubview:self.titleLabel];
    [self addSubview:self.contentTextView];
    [self addSubview:self.contentField];
    
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
      make.centerY.equalTo(self);
      make.right.equalTo(self.mas_left).offset(90*MIX_W);
    }];
    
    [self.contentField mas_makeConstraints:^(MASConstraintMaker *make) {
      make.centerY.equalTo(self);
      make.left.equalTo(self.titleLabel.mas_right).offset(40);
      make.right.equalTo(self).offset(-5);
    }];
    
    [self.contentTextView mas_makeConstraints:^(MASConstraintMaker *make) {
      make.centerY.equalTo(self);
      make.left.equalTo(self.titleLabel.mas_right).offset(40);
      make.right.equalTo(self).offset(-5);
      make.height.equalTo(self);
    }];
    
  }
  return self;
  
}

- (instancetype)initWithTitle:(NSString *)title fieldPlaceHolder:(NSString *)holder{
  SLLabelFeild *lf =  [self initWithtitle:title];
  lf.contentField.placeholder = holder;
  [lf.contentTextView removeFromSuperview];
  return lf;
}

- (instancetype)initWithTitle:(NSString *)title fieldText:(NSString *)field{
  SLLabelFeild *lf =  [self initWithtitle:title];
  lf.contentField.text = field;
  [lf.contentTextView removeFromSuperview];
  return lf;
}

- (instancetype)initWithTitle:(NSString *)title textViewHolder:(NSString *)holder{
  SLLabelFeild *lf =  [self initWithtitle:title];
  lf.contentTextView.text = holder;
  [lf.contentField removeFromSuperview];
  return lf;

}


- (void)enableNo{
  self.contentField.enabled = NO;
}


- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
  if ([string isEqualToString:@"\n"]){
    return YES;
  }
  
  NSString * aString = [textField.text stringByReplacingCharactersInRange:range withString:string];
  if (self.limit) {
    if ([aString length] > self.limit) {
      [SVProgressHUD showImage:nil status:@"已达输入上限"];
      return NO;
    }
  }
  

  return YES;
}


- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
  if (self.limit) {
    if (range.location > self.limit){
      [SVProgressHUD showImage:nil status:@"已达输入上限"];
      return  NO;
    }
  }
  
  
  return YES;
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
